package com.run.runbbs.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Component
public class MailUtils {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;
    Logger logger = LoggerFactory.getLogger(this.getClass());

    public void sendSimpleMail(String to,String title,String content) throws MailSendException {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(title);
            message.setText(content);
            mailSender.send(message);
            logger.info("邮件发送成功=>"+to);
    }
    //发送验证码邮件，返回验证码。
    public String sendCodeMail(String email){
        String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
        String content = "您的注册验证码为："+checkCode;
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(email);
        message.setSubject("注册验证码");
        message.setText(content);
        mailSender.send(message);
        logger.info("邮件发送成功=>"+email);
       return checkCode;
    }

    @Async
    public void sendWarnMail(String email, String indexType, String name, Date date){
        String content = "您的家人"+name+"在"+date+"输入的"+indexType+"异常,为了您的家人的身体健康，请及时了解其情况！";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(email);
        message.setSubject("家人指标异常警告");
        message.setText(content);
        mailSender.send(message);
        logger.info("邮件发送成功=>"+email);
    }

    public void sendAttachmentsMail(String to, String title, String cotent, List<File> fileList){
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(title);
            helper.setText(cotent);//helper.setTest(content,true)加true支持html
            String fileName = null;
            for (File file:fileList) {
                fileName = MimeUtility.encodeText(file.getName(), "GB2312", "B");
                helper.addAttachment(fileName, file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mailSender.send(message);
        logger.info("邮件发送成功");
    }

}

